<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addShow">添加分类</el-button>
      </div>
      <el-table :data="artList" style="width: 100%" border stripe>
        <el-table-column type="index" label="序号" width="100"></el-table-column>
        <el-table-column prop="cate_name" label="分类名称"></el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="updateCateBtnFn(scope.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="delCateFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog title="提示" :visible.sync="addVisible" @closed="onAddClosedFn" width="45%">
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="cancelFn">取 消</el-button>
        <el-button size="mini" type="primary" @click="okFn">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getArtCateListAPI, addArtCateAPI, updateArtCateAPI, delArtCateAPI } from '@/api/index'
export default {
  name: 'ArtCate',
  data () {
    return {
      // 文章分类列表
      artList: [],
      addVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      isEdit: false, // true编辑，false为新增
      editId: ''
    }
  },
  methods: {
    // 后台重新拉取刷新
    async initgetArtCateListAPI () {
      const res = await getArtCateListAPI()
      console.log('获取文章接口后返回值')
      console.log(res)
      this.artList = res.data.data
      console.log('artlist')
      console.log(this.artList)
    },
    // 分类按钮点击事件
    addShow () {
      this.addVisible = true
      this.isEdit = false
      this.editId = ''
    },
    // 取消
    cancelFn () {
      this.addVisible = false
    },
    // 添加操作
    async okFn () {
      // true为修改，false为新增
      if (this.isEdit) {
        // 编辑状态
        // 调用接口传递数据给后台
        const { data: res } = await updateArtCateAPI({ id: this.editId, ...this.addForm })
        if (res.code !== 0) return this.$message.error('更新分类失败！')
        this.$message.success('更新分类成功！')
      } else {
        // 新增
        const res = await addArtCateAPI(this.addForm)
        console.log(res)
        if (res.data.code !== 0) return this.$message.error(res.data.message)
        this.$message.success('添加分类成功')
      }
      // 重新请求列表数据
      this.initgetArtCateListAPI()
      // 关闭对话框
      this.addVisible = false
    },
    // 监听对话框关闭事件
    onAddClosedFn () {
      this.addForm.cate_name = ''
      this.addForm.cate_alias = ''
    },
    // 修改事件(先做数据回显)
    updateCateBtnFn (obj) {
      this.addVisible = true
      this.isEdit = true
      this.editId = obj.id
      this.addForm.cate_name = obj.cate_name
      this.addForm.cate_alias = obj.cate_alias
    },
    // 删除按钮
    delCateFn (obj) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delArtCateAPI(obj.id)
          if (res.data.code !== 0) return this.$message.error('删除失败')
          // this.$message.success('删除成功')
          // 重新拉取
          this.initgetArtCateListAPI()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  created () {
    this.initgetArtCateListAPI()
  }
}
</script>

  <style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
